package org.zht.dormpro.auth.bean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class PageBean extends HashMap<String, Object> {
    @Getter @Setter
    private Integer offset;
    @Getter @Setter
    private Integer limit;
    @Getter @Setter
    private Integer pageNum;
    @Getter @Setter
    private Integer total;
    public PageBean(Map<String, Object> params) {
        if (CollectionUtil.isNotEmpty(params)) {
            this.putAll(params);
            if (params.containsKey("offset")) {
                this.offset = Integer.parseInt(params.get("offset").toString());
                this.put("offset", offset);
            }
            if (params.containsKey("limit")) {
                this.limit = Integer.parseInt(params.get("limit").toString());
                this.put("limit", limit);
            }
            if (params.containsKey("pageNumber")) {
                this.pageNum = Integer.parseInt(params.get("pageNumber").toString());
                this.put("pageNum", pageNum);
            }
            if(params.containsKey("conditions")){
                String conditions = this.get("conditions").toString();
                JSONObject object = JSON.parseObject(conditions);
                Set<String> keys = object.keySet();
                for (String key : keys) {
                    Object o = object.get(key);
                    if(o!=null && StrUtil.isNotBlank(o.toString())){
                        this.putAll(object);
                    }
                }
                this.remove("conditions");

            }
        }
    }


}
